package test;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 任务线程池创建
 * @author zhouxiaolong@hzed.com
 * @date 创建日期:2017年12月28日 下午14:43:59
 * @version 1.0
 */
public class ThreadPoolUtil {
	
	private static final int CORE_NUM = Runtime.getRuntime().availableProcessors();  
	
	//推送审核结果线程池
	public static ThreadPoolExecutor pushAuditResultTask = ThreadPoolUtil.createArrayBlockThreadPool(8,"push-audit-result-task");
	//资料归档线程池
	public static ThreadPoolExecutor riskArchiveTask = ThreadPoolUtil.createArrayBlockThreadPool(16,"risk-archive-task");

	/**
	 * 创建无界ThreadPoolExecutor
	 * @param namePrefix 线程名称前缀
	 */
	public static ThreadPoolExecutor createLinkedBlockThreadPool(String namePrefix) {
    	int threadNum = CORE_NUM%2==0? CORE_NUM/2: CORE_NUM/2+1;
		return createLinkedBlockThreadPool(threadNum,namePrefix);
	}	
	
	/**
	 * 创建有界ThreadPoolExecutor
	 * @param namePrefix 线程名称前缀
	 */
	public static ThreadPoolExecutor createArrayBlockThreadPool(String namePrefix) {
    	int threadNum = CORE_NUM%2==0? CORE_NUM/2: CORE_NUM/2+1;
		return createArrayBlockThreadPool(threadNum,namePrefix);
	}

	/**
	 * 创建有界ThreadPoolExecutor
	 * @param namePrefix 线程名称前缀
	 */
	public static ThreadPoolExecutor createArrayBlockThreadPool2(String namePrefix) {
		int threadNum = CORE_NUM%2==0? CORE_NUM/2: CORE_NUM/2+1;
		if (threadNum < 16) {
			threadNum = 16;
		}
		return createArrayBlockThreadPool(threadNum,namePrefix);
	}
  	
   
	/**
	 * 创建无界ThreadPoolExecutor
	 * @param threadNum 线程数
	 */
	public static ThreadPoolExecutor createLinkedBlockThreadPool(int threadNum){
		return createLinkedBlockThreadPool(threadNum,"task-thread");
	}
	
	/**
	 * 创建有界ThreadPoolExecutor
	 * @param threadNum 线程数
	 * @param namePrefix 线程名称前缀
	 */
	public static ThreadPoolExecutor createLinkedBlockThreadPool(int threadNum,String namePrefix){
		int corePoolSize = threadNum%2==0? threadNum/2: threadNum/2+1;
		return new ThreadPoolExecutor(corePoolSize, threadNum, 60L, TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(1000));
	}	
		
	
	/**
	 * 创建有界ThreadPoolExecutor
	 * @param threadNum 线程数
	 */
	public static ThreadPoolExecutor createArrayBlockThreadPool(int threadNum) {
		return createArrayBlockThreadPool(threadNum,"task-thread");
	}
	
	/**
	 * 创建有界ThreadPoolExecutor
	 * @param threadNum 线程数
	 * @param namePrefix 线程名称前缀
	 */
	public static ThreadPoolExecutor createArrayBlockThreadPool(int threadNum,String namePrefix){
		int corePoolSize = threadNum%2==0? threadNum/2: threadNum/2+1;
		return new ThreadPoolExecutor(corePoolSize, threadNum, 60L, TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(1000));
	}	
	
}
